package com.cskaoyan.service;

import com.cskaoyan.bean.bo.ObjectStorageBo;
import com.cskaoyan.bean.pojo.ObjectStorage;
import com.cskaoyan.bean.pojo.ObjectStorageExample;
import com.cskaoyan.bean.vo.StorageListVo;
import com.cskaoyan.mapper.ObjectStorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Transactional(rollbackFor = Exception.class)
@Service
public class ObjectStorageServiceImpl implements ObjectStorageService {

    @Autowired
    ObjectStorageMapper objectStorageMapper;

    //对象上传
    @Override
    public ObjectStorage uploadFile(MultipartFile file) {
        ObjectStorage objectStorage = new ObjectStorage ();
        String path = "D:\\mallpictures";
        String uuid = UUID.randomUUID ().toString ();
        Date addTime = new Date ();
        String name = file.getOriginalFilename ();
        String newName = uuid + name;
        objectStorage.setAddTime (addTime)
                .setKey (name)
                .setType (file.getContentType ())
                .setName (newName)
                .setUpdateTime (addTime)
                .setUrl ("http://localhost:8083/pic/" + newName)
                .setSize (file.getSize ());

        int insert = objectStorageMapper.insertSelective (objectStorage);


        File newFile = new File (path, newName);

        try {
            file.transferTo (newFile);
        } catch (IOException e) {
            return null;
        }
        return objectStorage;
    }

    //查询
    @Override
    public StorageListVo selectStorageListVo(ObjectStorageBo objectStorageBo) {
        //分页助手
        PageHelper.startPage(objectStorageBo.getPage (),objectStorageBo.getLimit ());
        ObjectStorageExample objectStorageExample = new ObjectStorageExample ();
        objectStorageExample.setOrderByClause (objectStorageBo.getSort () + " " + objectStorageBo.getOrder ());
        ObjectStorageExample.Criteria criteria = objectStorageExample.createCriteria ();

        //逻辑删除,不查询
        criteria.andDeletedEqualTo (false);
        //根据对象key模糊查询

        if (objectStorageBo.getKey () != null) {
            criteria.andKeyLike ("%" + objectStorageBo.getKey () + "%");
        }
        //根据对象名称模糊查询

        if (objectStorageBo.getName () != null) {
            criteria.andNameLike ("%" + objectStorageBo.getName () + "%");
        }

        List<ObjectStorage> objectStorages = objectStorageMapper.selectByExample (objectStorageExample);
        System.out.println (objectStorages);

        PageInfo<ObjectStorage> pageInfo = new PageInfo<> (objectStorages);
        long total = pageInfo.getTotal ();

        StorageListVo storageListVo = new StorageListVo ();
        storageListVo.setItems (objectStorages)
                .setTotal (total);
//        if (total==0){
//            return null;
//        }
        return storageListVo;
    }

    //修改对象文件
    @Override
    public ObjectStorage updateObjectStorage(ObjectStorage storage) {
        int i = objectStorageMapper.updateByPrimaryKey (storage);
        return storage;
    }

    //删除对象文件
    @Override
    public int deleteObjectStorage(ObjectStorage storage) {
        //逻辑删除
        storage.setDeleted (true);
        int i = objectStorageMapper.updateByPrimaryKey (storage);
        return i;
    }


}
